//
//  PickViewSelect.m
//  时间选择器
//
//  Created by beijingduanluo on 16/1/7.
//  Copyright © 2016年 beijingduanluo. All rights reserved.
//

#import "PickViewSelect.h"
#define Width [UIScreen mainScreen].bounds.size.width/320.0
#define Height [UIScreen mainScreen].bounds.size.height/568.0

@interface PickViewSelect ()
{
//    SelectPickerMode selectPicker;
    
}
@property(nonatomic,strong)UIView *contentView;

@end

@implementation PickViewSelect


- (id)initWithDataArr:(NSArray *)dateArr andRowIndex:(int)row
{
    if (self=[super init])
    {
        UIWindow * window = [UIApplication sharedApplication].keyWindow;
        _backView =[[UIView alloc]initWithFrame:CGRectMake(0,  0,ScreenW, ScreenH)];
        _backView.backgroundColor =[UIColor clearColor];
        _backView.alpha=0.4;
        [window addSubview:_backView];
        
        pathRow = row;
        
        
        _contentView=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenH, ScreenW, 220)];
//        _contentView.backgroundColor=[UIColor redColor];
        [window addSubview:_contentView];
        
//        NSLog(@"%ld",selectPicker);
        
        //给bview一个动画
        [UIView animateWithDuration:0.5 animations:^{
            
            _contentView.y=ScreenH-220;
            
        }];
        
        UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0,  0,ScreenW,30)];
        view.backgroundColor = COLOR_EVERYVIEWBG;
        [_contentView addSubview:view];
        //
        _pickView =[[UIPickerView alloc]initWithFrame:CGRectMake(0, view.bottom,ScreenW,_contentView.height-view.height)];
        _pickView.delegate = self;
        _pickView.dataSource = self;
        _pickView.backgroundColor = COLOR_EVERYVIEWBG;
        _pickView.showsSelectionIndicator = YES;
        [_contentView addSubview:_pickView];
        cancel =[[UIButton alloc]initWithFrame:CGRectMake(10,5,50,20)];
        [cancel addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
        
        [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancel setTitle:@"取消" forState:UIControlStateNormal];
        [view addSubview:cancel];
        
        sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-60,5,50,20)];
        [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [sureBtn addTarget:self action:@selector(sureBtn:) forControlEvents:UIControlEventTouchUpInside];
        [sureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [view addSubview:sureBtn];
        
        _pickDate=[dateArr mutableCopy];
        [_pickView reloadAllComponents];
        
        UITapGestureRecognizer *removeTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeTapEvent:)];
        [_backView addGestureRecognizer:removeTap];
    }
    return self;
}


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;

}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickDate.count;
        
}

//每列每行的内容
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *soundLabel=[[UILabel alloc]init];
    
    soundLabel.backgroundColor = [UIColor whiteColor];//
    soundLabel.textColor=[UIColor blackColor];
    soundLabel.textAlignment=NSTextAlignmentCenter;
    
    soundLabel.text=_pickDate[row];
    dateStr = [NSString stringWithFormat:@"%@",_pickDate[row]];

    return soundLabel;
    
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30*Height;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    dateStr = [NSString stringWithFormat:@"%@",_pickDate[row]];
    selectedRow = (int)row;

}

-(void)removeTapEvent:(UITapGestureRecognizer *)tap
{
    //给bview一个动画
    [UIView animateWithDuration:0.5 animations:^{
        _contentView.y=ScreenH;
    }];
    [self removeFromSuperview];
    [_backView removeFromSuperview];
}

-(void)cancel:(UIButton *)btn
{
    //给bview一个动画
    [UIView animateWithDuration:0.5 animations:^{
        _contentView.y=ScreenH;
    }];
    [_backView removeFromSuperview];
    [self removeFromSuperview];
}

-(void)sureBtn:(UIButton *)btn
{
    
    NSLog(@"dateStr%@--%d",dateStr,pathRow);
    
    if ([self.delegate respondsToSelector:@selector(pickViewdelegateWith: andRow: andSelectecRow:)]) {
        [self.delegate pickViewdelegateWith:dateStr andRow:pathRow andSelectecRow:selectedRow];

        [UIView animateWithDuration:0.5 animations:^{
            _contentView.y=ScreenH;
        }];
        [_backView removeFromSuperview];
        [self removeFromSuperview];
    }
}

@end
